<template>
  <div class="register">
    <div class="register-box">
      <div class="form-header">
        <p>注册</p>
      </div>
      <el-form
        :model="registerForm"
        :rules="registerFormRules"
        ref="registerFormRef"
      >
        <el-form-item label="" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名,只支持英文大小写"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="registerForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="">
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addUser } from "@/api/login";
export default {
  name: "Register",
  data() {
    return {
      registerForm: {
        username: "",
        password: "",
      },
      registerFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "focus" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "focus",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "focus" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "focus",
          },
        ],
      },
    };
  },
  mounted() {},
  created() {},
  methods: {
    register() {
      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await addUser(this.registerForm);
        if (res.status === 0) {
          this.$message.success("注册成功,两秒后将自动跳转到登录页");
          setTimeout(() => {
            this.$router.push({ path: "/login" });
          }, 2000);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.register {
  height: 100%;
  background-image: -moz-linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  background-image: -webkit-linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  background-image: linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  .form-header {
    display: flex;
    justify-content: space-between;
    padding: 20px 0;
    margin: 0 5px;

    p {
      margin: 0;
      font-weight: 600;
      font-size: 25px;
      color: #333;
    }
  }

  .register-box {
    position: absolute;
    width: 300px;
    height: 280px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 50px;
    border-radius: 20px;
    background: #fff;

    .el-form {
      .el-input,
      .el-button {
        width: 300px;
      }
    }
  }
}
</style>
